<template>
  <!-- 新增视频库 -->
  <el-dialog
    title="添加文件夹"
    :visible.sync="addDirModal"
    width="550px"
    :modal-append-to-body="false"
  >
    <div style="box-sizing: border-box;padding: 0 40px;">
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-position="top"
        class="demo-ruleForm"
      >
        <el-form-item label="名称" prop="categoryName">
          <el-input v-model="ruleForm.categoryName" autofocus class="white" />
        </el-form-item>
        <el-form-item label="文件夹类型" prop="fileType">
          <el-select
            v-model="ruleForm.fileType"
            placeholder="请选择"
            class="white w-100"
          >
            <el-option
              v-for="item in typeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer flex-e" style="margin-top: 40px;">
        <el-button
          size="small"
          @click="addDirModal = false"
        >取 消</el-button>
        <el-button
          type="primary"
          size="small"
          @click="submitForm('ruleForm')"
        >确 定</el-button>
      </span>
    </div>
  </el-dialog>
</template>
<script>
import { eAddCategory } from '@/api/element'
export default {
  props: {
    registory: {
      type: Object,
      default() {
        return {}
      }
    }
  },
  data() {
    return {
      addDirModal: false,
      typeList: [
        { label: '视频', value: 'video' },
        { label: '音频', value: 'audio' },
        { label: '文案', value: 'text' },
        { label: '图片', value: 'picture' }
      ],
      ruleForm: {
        categoryName: '',
        fileType: ''
      },
      rules: {
        categoryName: [
          { required: true, message: '请输入名称', trigger: 'blur' }
        ],
        fileType: [
          { required: true, message: '请选择类型', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  methods: {
    submitForm(formName = 'ruleForm') {
      this.$refs[formName].validate(async(valid) => {
        if (valid) {
          const res = await eAddCategory({ ...this.ruleForm, materialLibraryId: this.registory.id.toString() })
          if (res.success) {
            this.$message.success('添加成功')
            this.$emit('refresh')
          } else {
            this.$message.error(res.message)
          }
          // const fileLength = Math.ceil(Math.random() * 10)
          // this.$emit('get', { ...this.ruleForm, fileLength })
          this.addDirModal = false
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(formName = 'ruleForm') {
      this.$refs[formName].resetFields()
      this.addDirModal = false
    },
    open() {
      this.$nextTick(() => {
        if (this.$refs['ruleForm']) {
          this.$refs['ruleForm'].resetFields()
        }
      })
      this.addDirModal = true
    },
    close() {
      this.resetForm()
    }
  }
}
</script>
<style lang="scss" scoped></style>
